package com.example.song.wlms.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.song.wlms.R;
import com.example.song.wlms.bean.NewestData;
import com.example.song.wlms.widget.GlideCircleTransform;
import com.example.song.wlms.widget.GlideYuanjiaoTransform;

import java.util.List;

/**
 * Created by song on 2017/2/13.
 */

public class NewestAdapter extends BaseAdapter {
    private List<NewestData.DataBean> list;
    private Context context;

    public NewestAdapter(List<NewestData.DataBean> list, Context context) {
        this.list = list;
        this.context = context;
    }

    @Override
    public int getCount() {
        return list != null ? list.size() : 0;
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.newest_item, parent, false);
            holder = new ViewHolder();
            holder.imageView = (ImageView) convertView.findViewById(R.id.newest_image);
            holder.head = (ImageView) convertView.findViewById(R.id.newest_touxiang);
            holder.title = (TextView) convertView.findViewById(R.id.newest_title);
            holder.text = (TextView) convertView.findViewById(R.id.newest_text);
            holder.name = (TextView) convertView.findViewById(R.id.name);
            holder.time = (TextView) convertView.findViewById(R.id.newest_time);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        String s = list.get(position).getDescribe().replaceAll("\r\n", "");
        Glide.with(context).load(list.get(position).getHead_img()).transform(new GlideYuanjiaoTransform(context)).placeholder(R.mipmap.bg_cos_avatar).into(holder.imageView);
        Glide.with(context).load(list.get(position).getAvatar()).transform(new GlideCircleTransform(context)).placeholder(R.mipmap.bg_cos_avatar).into(holder.head);
        holder.title.setText(list.get(position).getTitle());
        holder.text.setText(s);
        holder.name.setText(list.get(position).getAuthor());
        holder.time.setText(list.get(position).getCome_from() + "天前");
        return convertView;
    }

    class ViewHolder {
        ImageView imageView, head;
        TextView title, text, name, time;
    }
}
